package MQ.HelloWorld;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

@SuppressWarnings("deprecation")
public class Recv {
	private final static String QUEUE_NAME = "hello";

	public static void main(String[] argv) throws Exception {

		// 创建连接连接到MabbitMQ
		ConnectionFactory factory = new ConnectionFactory();
		// 设置MabbitMQ所在主机ip或者主机名
		factory.setHost("127.0.0.1");
		factory.setUsername("yuanh");
		factory.setPassword("yuanh");
		factory.setPort(5672);
		factory.setVirtualHost("y_yuanh");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		System.out.println(" [*] 接收消息等待中，推出请按 CTRL+C");
		QueueingConsumer consumer = new QueueingConsumer(channel);
		channel.basicConsume(QUEUE_NAME, true, consumer);
		try {
			while (true) {
				QueueingConsumer.Delivery delivery = consumer.nextDelivery();
				String message = new String(delivery.getBody());
				System.out.println(" 接收 '" + message + "'");
			}
		} catch (Exception e) {
			channel.close();
			connection.close();
		}
	}
}
